package admin.forms;

import helpers.StringHelper;
import helpers.ValidateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.sun.org.apache.xpath.internal.compiler.Keywords;

import models.ArticleModel;

import entities.Article;

public class ArticleForm {
	public static TreeMap checkValid(HttpServletRequest request, Article article) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String published = (String) request.getParameter("published");
		String title = (String) request.getParameter("title");
		String content = (String) request.getParameter("content");
		
		if (title == null || title.isEmpty()) {
			checkValid.put("title", "Title is not empty!");
			article.setTitle("");
		} else {
			article.setTitle(title);
		}
		
		if (title != null) {
			String alias = StringHelper.replace(title);
			ArticleModel newsModel = ArticleModel.getInstance();
			if (!alias.isEmpty() && alias.equalsIgnoreCase(article.getAlias())) {
				article.setAlias(alias);
			} else if (alias.isEmpty() || newsModel.checkAliasExists(alias, article.getLanguage())) {
				checkValid.put("title", "Article with this title already exists.");
				article.setAlias(alias);
			} else {
				article.setAlias(alias);
			}
		}
		
		if (content == null || content.isEmpty()) {
			checkValid.put("content", "Content is not empty!");
			article.setContent("");
		} else {
			article.setContent(content);
		}
		
		if (published != null)
			article.setPublished(new Boolean(published));
		
		
		
		form.put("checkValid", checkValid);
		form.put("article", article);
		
		return form;
	}
}
